<script setup>
import { isArray } from "@vue/shared";
import {
  onMounted,
  reactive,
  ref,
  defineEmits,
  defineProps,
  provide,
  watch
} from "vue";
import {
  getSearchCondition,
  getwaterwayBridgeDistribution,
} from "../utils/apis";
// 筛选
const value = ref("");
const onSearch = (val) => {
  console.log(val);
};
const emit = defineEmits(["closeChooseBtn", "getChooseDataList"]);
// 存放选中的
const list = ref([]);
const activeVal = ref(0);
const riskLevelList = ref([]); //确定筛选
const byAccidentList = ref([]); //确定筛选
const byStandardList = ref([]); //确定筛选
const byTonsList = ref([]); //确定筛选
const byFacilityList = ref(""); //确定筛选
const governanceTypeList1 = ref("");
const governanceTypeList2 = ref("");
const governanceTypeList3 = ref("");
const governanceTypeList4 = ref("");
const riskLevelLists = ref([
  { name: "全部", val: 3, type: "riskLevelList" },
  { name: "高等级", val: 1, type: "riskLevelList" },
  { name: "其他等级", val: 2, type: "riskLevelList" },
]);
const byAccidentLists = ref([
  { name: "按三年被撞桥梁", val: 3, type: "byAccidentList" },
  { name: "近一年被撞桥梁", val: 1, type: "byAccidentList" },
]);
const byStandardLists = ref([
  { name: "达标", val: 0, type: "byStandardList" },
  { name: "不达标", val: 1, type: "byStandardList" },
]);
const byTonsLists = ref([
  { name: "千吨级以上等级", val: 0, type: "byTonsList" },
  { name: "千吨级以下桥梁(其他等级)", val: 1, type: "byTonsList" },
]);
const byFacilityLists = ref([
  { name: "实时净空高桥梁", val: 1, type: "byFacilityList" },
  { name: "防撞设施桥梁", val: 2, type: "byFacilityList" },
  { name: "实时净高及防撞兼备桥梁", val: 3, type: "byFacilityList" },
]);
// bridge_type=1
const highwayList = ref([
  { name: "自查", val: "0", type: "governanceTypeList1" },
  { name: "综合评估", val: "1", type: "governanceTypeList1" },
  { name: "集中整治", val: "2", type: "governanceTypeList1" },
]);
// bridge_type=2
const cityList = ref([
  { name: "自查", val: "0", type: "governanceTypeList2" },
  { name: "综合评估", val: "1", type: "governanceTypeList2" },
  { name: "集中整治", val: "2", type: "governanceTypeList2" },
]);
// bridge_type=3
const railwayList = ref([
  { name: "自查", val: "0", type: "governanceTypeList3" },
  { name: "综合评估", val: "1", type: "governanceTypeList3" },
  { name: "集中整治", val: "2", type: "governanceTypeList3" },
]);
// bridge_type=3
const other = ref([
  { name: "自查", val: "0", type: "governanceTypeList4" },
  { name: "综合评估", val: "1", type: "governanceTypeList4" },
  { name: "集中整治", val: "2", type: "governanceTypeList4" },
]);
const choose = (el) => {
  activeVal.value = el.val;
  let e = list.value.indexOf(el);
  if (e == -1) {
    list.value.push(el);
  } else {
    list.value.splice(e, 1);
  }
};
const data = ref({
  code: "440000",
});
// 确定按钮
const confirmBtn = () => {
  delete data.riskLevelList
  delete data.byAccidentList
  delete data.byTonsList
  delete data.byStandardList
  delete data.byFacilityList
  delete data.governanceTypeList1
  delete data.governanceTypeList2
  delete data.governanceTypeList3
  delete data.governanceTypeList4
  if(list.value.length>0){
    list.value.map((el) => {
    if (el.type == "riskLevelList") {
      riskLevelList.value.push(el.val);
      data.riskLevelList = Array.from(new Set(riskLevelList.value));
    } else if (el.type == "byAccidentList") {
      byAccidentList.value.push(el.val);
      data.byAccidentList = Array.from(new Set(byAccidentList.value));
    } else if (el.type == "byTonsList") {
      byTonsList.value.push(el.val);
      data.byTonsList = Array.from(new Set(byTonsList.value));
    } else if (el.type == "byStandardList") {
      byStandardList.value.push(el.val);
      data.byStandardList = Array.from(new Set(byStandardList.value));
    } else if (el.type == "byFacilityList") {
      byFacilityList.value = el.val;
      data.byFacilityList = byFacilityList.value;
    } else if (el.type == "governanceTypeList1") {
      governanceTypeList1.value += el.val;
      data.governanceTypeList1 = governanceTypeList1.value.split("").toString();
    } else if (el.type == "governanceTypeList2") {
      // governanceTypeList2.value = el.val;
      governanceTypeList2.value += el.val;
      // data.governanceTypeList2 = governanceTypeList2.value;
      data.governanceTypeList2 = governanceTypeList2.value.split("").toString();
    } else if (el.type == "governanceTypeList3") {
      governanceTypeList3.value = el.val;
      // data.governanceTypeList3 = governanceTypeList3.value;
      data.governanceTypeList3 = governanceTypeList3.value.split("").toString();
    } else if (el.type == "governanceTypeList4") {
      governanceTypeList4.value = el.val;
      // data.governanceTypeList4 = governanceTypeList4.value;
      data.governanceTypeList4 = governanceTypeList4.value.split("").toString();
    }
  });
  // 条件传给父组件
  emit('getChooseDataList',data)
  }else{
    emit('getChooseDataList',data)
  }
  list.value=[]
  selectedCity.value=''
  data.value={ code: "440000"}
};
// 取消按钮
const closeBtn=()=>{
  list.value=[]
  selectedCity.value=''
  data.code= "440000",
  emit('getChooseDataList',data)
}
const change = () => {
  // 关闭弹窗
  emit("closeChooseBtn", false);
};
const showPicker = ref(false);
const cityColumns = ref([]);
const result = ref();
const selectedValues = ref();
const fieldValue = ref();
const customFieldName = {
  text: "name",
  value: "code",
  children: "",
};
const onConfirm = ({ selectedValues }) => {
  showPicker.value = false;
  fieldValue.value = selectedValues[0].text;
};

const selectedCity = ref(""); //选中的城市
const showCityBox = ref(false);
const selectCityList = ref([]);
// 选中某个城市
const getCity = (val) => {
  // 选中关闭下拉框回显数据
  showCityBox.value = false;
  selectedCity.value = val.name;
  data.code = val.code;
};
// 点击显示城市菜单
const getCityListFn = () => {
  showCityBox.value = true;
};
// 城市列表
const getSearchConditionFn = () => {
  getSearchCondition().then((res) => {
    if (res.code == 200) {
      let prores = res.data.ListData[0].children;
      prores.map((el) => {
        selectCityList.value.push(el);
      });
    }
  });
};

onMounted(() => {
  getSearchConditionFn();
});
</script>

<template>
  <div class="chooseBox">
    <div class="scrollbox">
      <div class="titleBox">筛选</div>
      <div class="main">
        <div class="choosePlace">
          <div class="provincial">
            <div class="left">省级</div>
            <div class="right">
              <ul>
                <!-- <li @click="showPicker=true">请选择<span class="downicon">&#xe60b;</span></li> -->
                <li>广东省</li>
              </ul>
            </div>
          </div>
          <div class="city">
            <div class="left">地市</div>
            <div class="rightBox">
              <div class="item">
                <input
                  class="chooseInput"
                  type="text"
                  placeholder="请选择"
                  readonly="readonly"
                  v-model="selectedCity"
                  @click="getCityListFn"
                />
                <div class="optionBox" v-if="showCityBox">
                  <div class="optionItem">
                    <li
                      v-for="item in selectCityList"
                      @click="getCity(item)"
                      :key="item.code"
                    >
                      {{ item.name }}
                    </li>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 搜索框 -->
        <div class="search">
          <van-search
            v-model="value"
            show-action
            autocomplete="off"
            placeholder="航道名称 | 桥梁名称 | 船舶名称"
          >
            <template #action>
              <div @click="onSearch(value)" class="searchBtnBox">
                <span class="searchBtn">搜索</span>
              </div>
            </template>
          </van-search>
        </div>
        <div class="typeBox">
          <div class="top">
            <div class="dot"></div>
            <div class="text">按列入桥梁隐患治理3年行动实施方案的桥梁</div>
          </div>
          <div class="bottom">
            <div
              v-for="item in riskLevelLists"
              :key="item.val"
              @click="choose(item)"
              :class="list.indexOf(item) > -1 ? 'active' : 'bottomItem'"
            >
              {{ item.name }}
            </div>
          </div>
          <!-- 中间小框 -->
          <div class="littleBox">
            <div class="top">公路桥梁</div>
            <div class="little">
              <div
                v-for="item in highwayList"
                :key="item.val"
                @click="choose(item)"
                :class="list.indexOf(item) > -1 ? 'activeLittle' : 'littleItem'"
              >
                {{ item.name }}
              </div>
            </div>
            <div class="top">城市桥梁</div>
            <div class="little">
              <div
                v-for="item in cityList"
                :key="item.val"
                @click="choose(item)"
                :class="list.indexOf(item) > -1 ? 'activeLittle' : 'littleItem'"
              >
                {{ item.name }}
              </div>
            </div>
            <div class="top">铁路桥梁</div>
            <div class="little">
              <div
                v-for="item in railwayList"
                :key="item.val"
                @click="choose(item)"
                :class="list.indexOf(item) > -1 ? 'activeLittle' : 'littleItem'"
              >
                {{ item.name }}
              </div>
            </div>
            <div class="top">厂矿、楼盘登企业性质桥梁</div>
            <div class="little" style="margin-bottom: 13px">
              <div
                v-for="item in other"
                :key="item.val"
                @click="choose(item)"
                :class="list.indexOf(item) > -1 ? 'activeLittle' : 'littleItem'"
              >
                {{ item.name }}
              </div>
            </div>
          </div>
          <div class="top">
            <div class="dot"></div>
            <div class="text">按事故</div>
          </div>
          <div class="bottom">
            <div
              v-for="item in byAccidentLists"
              :key="item.val"
              @click="choose(item)"
              :class="list.indexOf(item) > -1 ? 'active' : 'bottomItem'"
            >
              {{ item.name }}
            </div>
          </div>
          <div class="top">
            <div class="dot"></div>
            <div class="text">按达标</div>
          </div>
          <div class="bottom">
            <div
              v-for="item in byStandardLists"
              :key="item.val"
              @click="choose(item)"
              :class="list.indexOf(item) > -1 ? 'active' : 'bottomItem'"
            >
              {{ item.name }}
            </div>
          </div>
          <div class="top">
            <div class="dot"></div>
            <div class="text">按吨位</div>
          </div>
          <div class="bottom">
            <div
              v-for="item in byTonsLists"
              :key="item.val"
              @click="choose(item)"
              :class="
                list.indexOf(item) > -1 ? 'activeSingle' : 'bottomItemSingle'
              "
            >
              {{ item.name }}
            </div>
          </div>
          <div class="top">
            <div class="dot"></div>
            <div class="text">按设施</div>
          </div>
          <div class="bottom">
            <div
              v-for="item in byFacilityLists"
              :key="item.val"
              @click="choose(item)"
              :class="list.indexOf(item) > -1 ? 'activeSing' : 'bottomItemSing'"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <!-- 底部确定按钮 -->
      <div class="bigBox">
        <div class="btnBox">
          <div class="left" @click="closeBtn">取消</div>
          <div class="right" @click="confirmBtn">确定</div>
        </div>
      </div>
    </div>
  </div>
  <van-popup
    v-model:show="showPicker"
    round
    position="bottom"
    :overlay-style="{ background: 'rgba(255,255,255,0)' }"
  >
    <van-picker
      :columns="cityColumns"
      :columns-field-names="customFieldName"
      @cancel="showPicker = false"
      @confirm="onConfirm"
    />
  </van-popup>
</template>

<style lang="scss" scoped>
.chooseBox {
  height: 100%;
  border-radius: 8px 8px 0 0;
  overflow: hidden;
  width: calc(100% + 6px);
  .scrollbox {
    height: 100%;
    width: calc(100% + 6px);
    overflow: scroll;
    .titleBox {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      // position: absolute;
      // top: 0;
      background: #fff;
      position: fixed;
      line-height: 40px;
      font-weight: bold;
      font-size: 16px;
      color: #000000;
      width: 100%;
      margin: 0 auto;
      padding-top: 16px;
      // z-index: 2;
      .title {
        font-weight: bold;
        width: 32px;
        height: 24px;
        padding: 16px;
        font-size: 16px;
        color: #000000;
        line-height: 24px;
        padding: 16px;
        height: 40px;
        text-align: center;
        line-height: 40px;
      }
    }
    .main {
      margin-top: 60px;
      margin-bottom: 90px;
      padding-right: 10px;
      .choosePlace {
        display: flex;
        .provincial {
          display: flex;
          justify-content: space-between;
          .left {
            width: 24px;
            height: 20px;
            font-size: 12px;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.9);
            line-height: 20px;
            margin-right: 3px;
            margin-left: 16px;
          }
          .right {
            width: 76px;
            height: 26px;
            background: rgba(0, 0, 0, 0.04);
            border-radius: 4px;
            ul {
              width: 100%;
              height: 100%;
              li {
                height: 26px;
                line-height: 26px;
                font-size: 12px;
                font-weight: 400;
                color: rgba(0, 0, 0, 0.4);
                line-height: 26px;
                padding-left: 10px;
                .downicon {
                  font-family: "iconfont";
                  width: 12px;
                  height: 6px;
                  color: #266fe8;
                  margin-left: 4px;
                }
              }
            }
          }
        }
        .city {
          display: flex;
          justify-content: space-between;
          .left {
            width: 24px;
            height: 20px;
            font-size: 12px;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.9);
            line-height: 20px;
            margin-right: 3px;
            margin-left: 16px;
          }
          .rightBox {
            width: 76px;
            height: 26px;
            .item {
              width: 100%;
              height: 26px;
              .chooseInput {
                height: 24px;
                line-height: 26px;
                outline: none;
                border: none;
                background: rgba(0, 0, 0, 0.04);
                width: 100%;
                font-size: 12px;
                font-weight: 400;
                color: rgba(0, 0, 0, 0.4);
                padding-left: 10px;
                border-radius: 4px;
              }
              .optionBox {
                height: 80px;
                width: 88px;
                background: #ffffff;
                position: relative;
                overflow: hidden;
                z-index: 10;
                padding-bottom: 12px;
                .optionItem {
                  position: absolute;
                  height: auto;
                  width: calc(100% + 8px);
                  height: calc(100% + 8px);
                  overflow: scroll;
                  top: 0;
                  background: #fff;
                  li {
                    margin: 0;
                    padding-left: 8px;
                    list-style: none;
                    color: #000000;
                    background: #fff;
                    font-size: 12px;
                    padding-bottom: 4px;
                  }
                }
              }
            }
          }
        }
      }
      .search {
        background: rgba(0, 0, 0, 0.04);
        margin-left: 16px;
        // margin-right: 10px;
        ::v-deep .van-search--show-action {
          padding-left: 0;
          padding-right: 0;
          .van-search__content {
            height: 26px;
            background: rgba(0, 0, 0, 0.04);
            .van-search__field {
              height: 26px;
              line-height: 26px;
              .van-field__control {
                font-size: 12px;
                line-height: 26px;
              }
            }
          }
        }

        .searchBtnBox {
          width: 56px;
          height: 26px;
          background: #266fe8;
          border-radius: 4px 4px 4px 4px;
          .searchBtn {
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            font-size: 12px;
            font-weight: 400;
            line-height: 26px;
          }
        }
      }
      .typeBox {
        .top {
          margin-top: 4px;
          display: flex;
          align-items: center;
          .dot {
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: #266fe8;
            margin-left: 16px;
            margin-right: 6px;
          }
          .text {
            // width: 236px;
            height: 20px;
            font-size: 12px;
            font-family: PingFang SC-Regular, PingFang SC;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.9);
            line-height: 20px;
          }
        }
        .bottom {
          display: flex;
          flex-wrap: wrap;
          .bottomItem,
          .bottomItemSingle,
          .bottomItemSing {
            padding: 3px 28px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.6);
            line-height: 26px;
            height: 26px;
            background: rgba(0, 0, 0, 0.06);
            margin-left: 16px;
            margin-top: 4px;
            border-radius: 4px 4px 4px 4px;
            opacity: 1;
          }
          .bottomItem:first-of-type {
            margin-left: 28px;
          }
          .bottomItemSingle:first-of-type {
            margin-left: 28px;
          }
          .bottomItemSingle:last-of-type {
            padding: 3px 22px;
          }
          .bottomItemSing:first-of-type {
            margin-left: 28px;
          }
          .bottomItemSing:last-of-type {
            margin-left: 28px;
          }
          .active,
          .activeSingle,
          .activeSing {
            background: rgba(38, 111, 232, 0.6);
            padding: 3px 28px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.6);
            line-height: 26px;
            height: 26px;
            margin-left: 16px;
            margin-top: 4px;
            border-radius: 4px 4px 4px 4px;
            opacity: 1;
            color: #fff;
          }
          .active:first-of-type {
            margin-left: 28px;
          }

          .activeSingle:first-of-type {
            margin-left: 28px;
          }
          .activeSingle:last-of-type {
            padding: 3px 22px;
          }

          .activeSing:first-of-type {
            margin-left: 28px;
          }
          .activeSing:last-of-type {
            margin-left: 28px;
          }
        }
        .littleBox {
          border-radius: 8px 8px 8px 8px;
          opacity: 1;
          border: 1px solid rgba(0, 0, 0, 0.1);
          margin: 14px 16px 13px 28px;
          .top {
            margin-top: 14px;
            margin-left: 16px;
            font-size: 12px;
            font-family: PingFang SC-Regular, PingFang SC;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.9);
          }
          .little {
            display: flex;
            flex-wrap: wrap;
            .littleItem {
              padding: 3px 24px;
              font-size: 12px;
              font-weight: 400;
              color: rgba(0, 0, 0, 0.6);
              line-height: 26px;
              height: 26px;
              background: rgba(0, 0, 0, 0.06);
              margin-left: 16px;
              margin-top: 4px;
              border-radius: 4px 4px 4px 4px;
              opacity: 1;
            }
          }
          .activeLittle {
            background: rgba(38, 111, 232, 0.6);
            padding: 3px 24px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.6);
            line-height: 26px;
            height: 26px;
            margin-left: 16px;
            margin-top: 4px;
            border-radius: 4px 4px 4px 4px;
            opacity: 1;
            color: #fff;
          }
        }
      }
    }

    .bigBox {
      height: 40px;
      position: fixed;
      bottom: 67px;
      background: #fff;
      z-index: 2;
      padding-bottom: 16px;
      width: 100%;
      .btnBox {
        justify-content: center;
        align-items: center;
        display: flex;
        margin-bottom: 20px;
        .left {
          width: 163px;
          height: 40px;
          border-radius: 4px 4px 4px 4px;
          opacity: 1;
          border: 1px solid rgba(0, 0, 0, 0.2);
          font-size: 14px;
          font-weight: 500;
          color: rgba(0, 0, 0, 0.4);
          text-align: center;
          line-height: 40px;
          background-color: #fff;
        }
        .right {
          width: 164px;
          height: 40px;
          background: rgba(38, 111, 232, 0.9);
          border-radius: 4px 4px 4px 4px;
          opacity: 1;
          font-size: 14px;
          font-weight: 500;
          color: #fff;
          margin-left: 16px;
          text-align: center;
          line-height: 40px;
        }
      }
    }
  }
}
::v-deep .van-cell {
  padding: 0;
}
::v-deep .van-field__body {
  background: rgba(0, 0, 0, 0.04);
}
</style>